var $_GET = (function () {
    var url = window.document.location.href.toString();
    console.log('GET info' , url)
    var u = url.split("?");
    if (typeof (u[1]) == "string") {
        u = u[1].split("&");
        var get = {};
        for (var i in u) {
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/property/findByCommodityId?commodityid='+vm.id,
        datatype: "json",
        colModel: [
            { label: 'id', name: 'tbCommodityProperty.id', index: 'tbCommodityProperty.id', width: 50, key: true },
            { label: '创建时间', name: 'tbCommodityProperty.createdate', index: 'tbCommodityProperty.createDate', width: 80 },
            { label: '属性名', name: 'tbProperty.propertyname', index: 'tbProperty.propertyName', width: 80 },
            { label: '属性值', name: 'tbProperty.propertyvalue', index: 'tbProperty.propertyValue', width: 80 },
            { label: '库存', name: 'tbCommodityProperty.propertynum', index: 'tbCommodityProperty.propertyNum', width: 80 },
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "listdto",
            // page: "page.current",
            // total: "page.pages",
            // records: "page.total"
        },
        prmNames : {
            page:1,
            limit:1000
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            propertyname:null
        },
        showList: true,
        title: null,
        property: {},

        //商品ID
        id:null,
        //商品名称
        name:null,

        //所有属性名列表
        allPropertyNameList:[],

        //选中属性名对应的属性值列表
        // allPropertyValList:[],

        //新增属性对象
        addPostData:{
            //商品ID ， 选中的属性ID ，库存
            propertyId:0,
            commodityId:0,
            num:0
        },

        commodityPropertyList:[]
    },
    mounted:function(){
        this.id = $_GET['id'];
        this.addPostData.commodityId = this.id;
        if(!this.id){
            alert('抱歉，没有获取到商品ID');
            return false;
        }

        console.log('商品属性页面渲染成功！' ,this.id);

        this.getAllPropertyNameList()
    },
    methods: {
        // findByCommodityId:function(id) {
        //     //获取商品的属性列表
        //     $.ajax({
        //         type: "GET",
        //         url: baseURL + 'sys/property/findByCommodityId',
        //         contentType: "application/json",
        //         data: ({commodityid:Number(id)}),
        //         success: function(r){
        //             if(r.code === 0){
        //                 console.log('获取商品的属性列表' , r)
        //                 vm.commodityPropertyList = r.list
        //             }else{
        //                 alert(r.msg);
        //             }
        //         }
        //     });
        // },
        handleNum:function () {
          if(vm.addPostData.num < 0){
              vm.addPostData.num = 0;
          }
        },
        getAllPropertyNameList:function(){
            //获取全部属性名
            var url = "sys/property/findAll";
            $.ajax({
                type: "GET",
                url: baseURL + url,
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code === 0){
                        vm.allPropertyNameList = r.list
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.property = {};

            //清空原有数据
            vm.addPostData.num = 0;
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.property.id == null ? "sys/property/saveCommodityProperty" : "sys/property/updateCommodityProperty";

            vm.addPostData.commodityId = Number(vm.addPostData.commodityId)
            vm.addPostData.num = Number(vm.addPostData.num)

            //update
            var __updateObj = vm.addPostData
            __updateObj.id = vm.property.id

            $.ajax({
                type: vm.property.id == null ? "GET" : "GET",
                url: baseURL + url,
                contentType: "application/json",
                data:vm.property.id == null ? vm.addPostData : (__updateObj),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/commodityproperty/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(baseURL + "sys/commodityproperty/info/"+id, function(r){
                vm.property = r.commodityProperty;
                vm.addPostData = {
                    //商品ID ， 选中的属性ID ，库存
                    propertyId:vm.property.propertyid,
                    commodityId:vm.property.commodityid,
                    num:vm.property.propertynum
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'propertyname': vm.q.propertyname},
                page:page
            }).trigger("reloadGrid");
        },
        back() {
            window.history.go(-1);
        }
    }
});